package org.budo.beetl.error.handler.web;

import java.io.IOException;
import java.io.Writer;

import org.beetl.core.exception.BeetlException;
import org.beetl.ext.web.WebErrorHandler;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

/**
 * @author li
 */
public class BudoWebBeetlErrorHandler extends WebErrorHandler {
    protected static final String _VERSION = "<a target=\"_blank\" href=\"http://limw.cn\" class=\"powered\">Powered by limw.cn</a>";

    protected static final String _DEV_MODEL_ERROR = "<html><head><title>beetlException</title>" //
            + DEV_MODEL_STYLE //
            + "</head><body><div class='main'><h1>[title]</h1>" // 
            + "<HR size='1' noshade='noshade' class='t1'><xmp style='padding-left:20px'>[message]</xmp>" // 
            + "<HR size='1' noshade='noshade'><h3>" //
            + _VERSION //
            + "</h3><div></body></html>";

    private static final Logger log = Slf4j.getLogger();

    @Override
    public void processExcption(BeetlException e, Writer writer) {
        log.error("#29 processExcption, e=" + e, e);
        super.processExcption(e, writer);
    }

    @Override
    protected void render(Writer writer, String title, String message) {
        try {
            log.error("#36 render, message=\n" + message);

            writer.write(_DEV_MODEL_ERROR.replace("[title]", title).replace("[message]", message));
        } catch (IOException e) {
            log.error("#40 render error, e=" + e, e);
        }
    }
}